/**
 * 
 * @author winzter143
 * 
 */

//jQuery(document).ready(function() {
//});


/**
 * 
 * @param {type} w
 * @returns {undefined}
 */

(function (w ) {
    
    // Extended jQuery Function
    w.fn.extend({
        copyPlaceholder: function(_ua) {
            return this.each(function() {
                console.log("copyPlaceholder");
                this.type='text';
                // If UA if IE9, factsheet IE not support place holder.
                if(_ua.search("MSIE 9")){
                    this.value=w(this).attr('placeholder');
                    console.log("copyPlaceholder IE: "+this.name+" => "+this.value);
                }else{
                    this.value=this.placeholder;
                    console.log("copyPlaceholder SAfari: "+this.name+" => "+this.value);
                }
            });
        }
    });

    w.fn.wLogin = function(options) {
        // These are the defaults.
        var s = w.extend({
            _currentPrice:"",
            _currentBrand: "",
            _lastFilter:""
        }, options );
        var _ua = navigator.userAgent;
        
        var methods = {
            login: function(user, password){
                return true;
            },
            toggleShowPassword:function (options) {
                
                var settings = w.extend({
                    field: "#password",
                    control: "#toggle_show_password"
                }, options);

                var control = w(settings.control);
                var field = w(settings.field);

                control.bind('click', function () {
                    if (control.is(':checked')) {
                        field.attr('type', 'text');
                    } else {
                        field.attr('type', 'password');
                    }
                });
            },
            isEmtpy: function(elem){
                if (w.trim(elem.val()).length === 0) {
                    elem.addClass("error");
                    return true;
                } else {
                    elem.removeClass("error");
                    return false;
                }
                
            },// isEmtpy
        }; // methods
        
        /**
         * NOTE: This was trigger even in the form submition(unexpected)
         */
        w('.buttonBlue').click(function(){
            var url = w(this).attr('href');
            if ((url !== "")&&(typeof url !== 'undefined')){
                document.location=w(this).attr('href');
            }
        });
            
        
        w("#frm_login").submit(function( event ) {
            var _user = w('input[name=txt_email]'),
                _pass= w('input[name=txt_pass]');
            
            if (_ua.search("Safari") >= 0 && _ua.search("Chrome") < 0) {
                w(this).find('[placeholder]').each(function() {
                    if (w(this).val() == w(this).attr("placeholder")) {
                        w(this).val("");
                    }
                });
   
            }
            
            var r=true;
            w('#error-msg').hide();
            
            if (methods.isEmtpy(_user)){
               w('#txt_email').show();
                _user.focus();
                
                r=false;
            }else{
                w('#txt_email').hide();
            }
            if (methods.isEmtpy(_pass)){
                _pass.focus();
				if (r==false){_user.focus();}
                w('#txt_pass').show();
                r=false;
            }else{ 
                // remove message
                w('#txt_pass').hide();
            }
            console.log(r);
            if (r===false) resizeIframe();
            return r; 		
        });
		//w(".input-placehorder").copyPlaceholder();
        //w('#btn_login').click(function(){
        //    w("#frm_login").submit();
        //});
		
        /**
         * IE and Safari didnt support placeholder into center text.
         * As workaround, change place holde into text.
         */ 
        if ((_ua.search("MSIE 9")||_ua.search("Safari")) && _ua.search("Chrome") < 0) {	
            var $username = w("#username");
            var $password = w("#password");

            console.log("IE 9/Safari");
//            if($password.val() == $password.attr("placeholder")) {
//                $password.attr("type","text");
//            } else {
//                $password.attr("type","password");
//            }
            // Initially copy the placeholder into text value
            //w(".input-placehorder").attr("type","text").val(w('.input-placehorder').attr('placeholder'));
            
			w(".input-placehorder")
            .copyPlaceholder(_ua)
            .focus(function () {
                if (w(this).val() == w(this).attr("placeholder")){
                    w(this).val("");
                }
            })
            .blur(function(){
                if (w(this).val() == ""){
                    w(this).val(w(this).attr("placeholder"));
                }
                if(w(this).val() == w(this).attr('placeholder')) {	
                    w(this).css("color","gray");
                    if (w(this).attr('ftype')==='password'){
                        w(this).attr("type","text");
                    }
                } else {
                    w(this).css("color","black");
                    if (w(this).attr('ftype')==='password'){
                        w(this).attr("type","password");
                    }

                }
            })
            .keypress(function() {
                if (w.trim(w(this).val()).length > -1) {
                    w(this).css("color","black");
                    if (w(this).attr('ftype')==='password'){
                        w(this).attr("type","password");
                    }
                } else {
                    w(this).css("color","gray");
                    if (w(this).attr('ftype')==='password'){
                        w(this).attr("type","text");
                    }
                }                
            })
            .on('paste', function () {
                if(w(this).val() == w(this).attr('placeholder')) {	
                    w(this).css("color","gray");
                    if (w(this).attr('ftype')==='password'){
                        w(this).attr("type","text");
                    }
                } else {
                    w(this).css("color","black");
                    if (w(this).attr('ftype')==='password'){
                        w(this).attr("type","password");
                    }
                }
            })
            .css("color","gray"); 
        }
		
        return methods;
    };
    
    
//    $(function(){
//        $.toggleShowPassword({
//            field: '#tianggeeye1',
//            control: '#tianggeeye2'
//        });
//    });

    w(document).ready(function(){
        var login = w(document).wLogin();
        
        /* login.toggleShowPassword({
            field: '#tianggeeye1',
            control: '#tianggeeye2'
        }); */
        //console.log("wDocument Ready");
    });
    w(window).ready(function(){
       resizeIframe();
    });
    

}( jQuery ));